package com.gk.carshopsys.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gk.carshopsys.dao.AdminDao;
import com.gk.carshopsys.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;


    //根据id和用户名查找用户
    @Override
    public List<Admin> findAdminByIdAndUsername(Integer id,String username) {
        return adminDao.findAdminByIdAndUsername(id,username);
    }

    //登录方法
    @Override
    public Admin loginAdmin(String username, String password) {
        Admin admin=adminDao.findAdmin(username);

        if (admin!=null){
            String passwd = admin.getPassword();
            if(password.equals(passwd)){
                return admin;
            }
        }

        return null;
    }



/**
 *     添加用户
 *      -1 用户名重复
 *      1 插入成功
 *      0 数据库插入失败
 */
    @Override
    public int addAdmin(String username, String password) {
        Admin add_admin = new Admin(username,password,new Date(System.currentTimeMillis()));
        //查找是否重复
        List<String> admin= adminDao.findAdminUsername(username);
        if (!admin.isEmpty()){
            return -1;
        }
        //不重复返回数据库插入结果
        return adminDao.addAdmin(add_admin);
    }


    //修改用户信息
    @Override
    public int updateAdmin(Integer id,String username,String password) {
        List<String> admin= adminDao.findAdminUsername(username);
        if (admin.size()>1){
            return -1;
        }

        Admin update_admin = new Admin(id,username,password,null);
        return adminDao.updateAdmin(update_admin);
    }

    //批量删除用户
    @Override
    public int delAdmin(List<Integer> idlist) {
        return adminDao.delAdmin(idlist);
    }
}
